package com.utils;


import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;

@Slf4j
public class FRDTxtFileUtils {


    public  static Map<String,List<List<String>>> getInfo(String parantPath) {
        File fiels = new File(parantPath);
        final File[] txts = fiels.listFiles(pathname -> pathname.getName().contains("txt"));
        Map<String, List<List<String>>> returnMap = new HashMap<>();
        for (File txt : txts) {
            log.info("开始读取文件{}", txt.getName());
            // 使用随机读取
            RandomAccessFile fileRead = null;
            StringBuilder sbr = new StringBuilder();

            try {
                //使用读模式
                String readLineString = "";
                fileRead = new RandomAccessFile(txt, "r");
                fileRead.seek(0);
                List<List<String>> infoList = new ArrayList<>();

                while ((readLineString = fileRead.readLine()) != null) {
                    String lineInfo = new String(readLineString.getBytes("ISO-8859-1"), "utf-8");
                    String[] split = lineInfo.split("\\|~\\|");
                    if (split.length < 1) {
                        split = lineInfo.split("~");
                    }
                    List<String> readLineList = new ArrayList<>();
                    readLineList.addAll(Arrays.asList(split));
                    infoList.add(readLineList);
                }
                returnMap.put(txt.getName().contains("_2019") ? txt.getName().substring(0, txt.getName().indexOf("_2019")) : txt.getName(), infoList);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileRead != null) {
                    try {
                        //关闭资源
                        fileRead.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        return returnMap;
    }

    public static void main(String[] args) {
//        String parantPath="D:\\工作文档\\sinobase\\需求文档\\呼叫中心\\ATLAS";
//          Map<String, List<List<String>>> info = getInfo(parantPath);
//          log.info(info.size()+"--");
    }
}
